package com.jrtc.service.impl;

import com.jrtc.base.entity.bo.*;
import com.jrtc.base.util.MinioUtil;
import com.jrtc.dao.*;
import com.jrtc.service.HospitalService;
//import net.sourceforge.pinyin4j.PinyinHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("hospitalService")
public class HospitalServiceImpl implements HospitalService {
    @Resource
    private HospitalDao hospitalDao;
    @Resource
    private EquipmentDao equipmentDao;
    @Resource
    private MinioUtil minioUtil;
    @Resource
    private DoctorDao doctorDao;
    @Resource
    private RoleDao roleDao;

    /**
     * 医院列表
     * */
    @Override
    public List<Hospital> hospitalList() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Hospital> hospitalList = hospitalDao.hospitalList();
        if (hospitalList != null && hospitalList.size() > 0){
            for (Hospital hospital : hospitalList) {
                List<Equipment> equipmentList = equipmentDao.equipmentList(hospital.getId());
                if (equipmentList != null && equipmentList.size() > 0){
                    hospital.setEquipmentList(equipmentList);
                }
                if (hospital.getCreateTime() != null){
                    hospital.setCreate(format.format(hospital.getCreateTime()));
                }
            }
        }
        return hospitalList;
    }

    /**
     * 医院唯一号获取医院信息
     * */
    @Override
    public Hospital hospitalByNum(Integer num) {
        Hospital hospital = hospitalDao.hospitalByNum(num);
        Equipment equipment = equipmentDao.hospitalByNum(hospital.getId());
        hospital.setEquipmentId(equipment.getId());
        hospital.setEquipmentName(equipment.getEquipmentName());
        hospital.setCreateDate(equipment.getCreateTime());
        hospital.setEnable(equipment.getEnable());
        hospital.setEquipmentCode(equipment.getEquipmentCode());
        return hospital;
    }

    /**
     * 上传医院logo
     * */
    @Override
    public String uploadLogo(MultipartFile file) throws Exception {
        if (file != null){
            String source = "hospital";
            String business = "logo";

            String uploadFile;
            try {
                uploadFile = minioUtil.uploadFile(file, source, business);
            } catch (Exception e){
                throw new Exception("文件上传失败");
            }
            return uploadFile;
        }
        else {
            throw new Exception("没有接收到文件");
        }
    }

    /**
     * 添加机构
     * */
    @Transactional
    @Override
    public int insertHospital(Hospital hospital) {
        Integer hospitalCode = (int)((Math.random()*9+1)*100000);
        hospital.setHospitalCode(hospitalCode);
        int i = hospitalDao.insertHospital(hospital);
        if (i > 0){
            Doctor doctor = new Doctor();
            doctor.setDoctorName(hospital.getDoctorName());
//            String pinYinHeadChar = getPinYinHeadChar(hospital.getDoctorName());
//            doctor.setPassword(pinYinHeadChar + hospital.getPhone().substring(5));
            doctor.setRegisterTime(new Date());
            doctor.setAffiliatedHospital(hospital.getHospitalName());

//            roleDao.queryById();
//
//                        /**
//                         * 角色id
//                         * */
//                        @JsonProperty(value = "roleId")
//                        private Integer roleId;
//
//            /**
//             * 医院唯一号
//             * */
//            @JsonProperty(value = "hospitalCode")
//            private Integer hospitalCode;
//            doctorDao.insert(doctor);
        }
        return i;
    }

//    /**
//     * 获取中文首字母
//     * */
//    public static String getPinYinHeadChar(String str) {
//        StringBuilder convert = new StringBuilder();
//        for (int j = 0; j < str.length(); j++) {
//            char word = str.charAt(j);
//            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
//            if (pinyinArray != null) {
//                convert.append(pinyinArray[0].charAt(0));
//            } else {
//                convert.append(word);
//            }
//        }
//        String toUpperCase = convert.toString().toUpperCase();
//        return toUpperCase;
//    }
}